//
//  TextSpacingViewController.m
//  TotalDemo
//
//  Created by gujian on 16/2/29.
//  Copyright © 2016年 gujian. All rights reserved.
//

#import "TextSpacingViewController.h"
#import "TDViewUtils.h"
#import <CoreText/CoreText.h>

@interface TextSpacingViewController ()

@end

@implementation TextSpacingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupLabel];
    [self setupSpacingLbl];
}


- (void)setupLabel
{
    UILabel *characterSpacingLbl = [TDViewUtils createColorFullLabel];
    characterSpacingLbl.frame = CGRectMake(50, 50, SCREEN_WIDTH-2*50, 150);
    characterSpacingLbl.numberOfLines = 0;
    characterSpacingLbl.textAlignment = NSTextAlignmentLeft;
    characterSpacingLbl.lineBreakMode = NSLineBreakByCharWrapping;
    [self.view addSubview:characterSpacingLbl];
    
    NSString *str = @"幸运购是旨在为您和商家提供幸运购物服务的网络平台。您花费1元购买1幸运豆同时获赠1张游戏赠券，您可凭幸运豆参与相应数量的人次。商品的总需人次全部售出后，系统将根据规则计算出的一个幸运号码。持有该幸运号码的用户可直接获得该商品";
    
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:str];
    long number = 1.5f;
    CFNumberRef num = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt8Type, &number);
    [attributeStr addAttribute:(id)kCTKernAttributeName value:(__bridge id _Nonnull)(num) range:NSMakeRange(0, [attributeStr length]-1)];
    
    CFRelease(num);
    
    /*
         Nil: A null pointer to an Objective-C class.表示对类进行赋空值
         
         NULL: A null pointer to anything else,  is for C-style memory pointers. 用于对非对象指针赋空值，比如C指针
         
         NSNull 对于像NSArray这样的类型，nil或NULL不能做为加到其中的Object，如果定义了一个NSArray，为其分配了内存，又想设置其中的内容为空，则可以用[NSNULL null】返回的对象来初始化NSArray中的内容
     */
    
    
    characterSpacingLbl.text = nil;
    characterSpacingLbl.attributedText = attributeStr;
    
    
}

- (void)setupSpacingLbl
{
    UILabel *topLbl = [TDViewUtils createColorFullLabel];
    topLbl.text = @"共参与了：";
    [topLbl sizeToFit];
    topLbl.frame = CGRectMake(50, 250, topLbl.width, 30);
    [self.view addSubview:topLbl];
    
    UILabel *middleLbl = [TDViewUtils createColorFullLabel];
    middleLbl.text = @"中奖号码：";
    [middleLbl sizeToFit];
    middleLbl.frame = CGRectMake(50, topLbl.bottom+5, middleLbl.width, 30);
    [self.view addSubview:middleLbl];
    
    UILabel *bottomLbl = [TDViewUtils createColorFullLabel];
    bottomLbl.text = @"获得者：";
    [bottomLbl sizeToFit];
    bottomLbl.frame = CGRectMake(50, middleLbl.bottom+5, middleLbl.width, 30);
    [self.view addSubview:bottomLbl];
    
    [self adjustLabel:bottomLbl];
}


- (void)adjustLabel:(UILabel *)label
{
    CGRect orginRect = label.frame;
    
    NSString *str = label.text;
    label.textAlignment = NSTextAlignmentLeft;
     label.text = nil;
    
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:str];
    double number = 1.5;
    
    BOOL flag = NO;
    long countNum = 0;
    NSDate *beginDate = [NSDate date];
    while (!flag) {
        CFNumberRef num = CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &number);
        [attributeStr addAttribute:(id)kCTKernAttributeName value:(__bridge id _Nonnull)(num) range:NSMakeRange(0, [attributeStr length]-2)];
        
        CFRelease(num);
        label.attributedText = attributeStr;
        
        [label sizeToFit];
        if (label.width == orginRect.size.width) {
            flag = YES;
        }else
        {
            
            //算法复杂度高
            //实际上offWidth要大一些
            countNum++;
            CGFloat offWidth = orginRect.size.width - label.width;
            if (offWidth > 0)
            {
                
                number += (offWidth/label.width)*[UIScreen mainScreen].scale;
                LotLog(@"originRect.size.width:%f",orginRect.size.width);
                LotLog(@"width++++++++++%f",label.width);
                LotLog(@"offWidth+++++++++%f",offWidth);
                LotLog(@"number++++++++++++%f",number);
                LotLog(@"++++++++++++%ld",countNum);
            }else if (offWidth <0)
            {
                number -= (offWidth/label.width)*[UIScreen mainScreen].scale;
                LotLog(@"-------------%ld",countNum);
            }
            
            
           
        }
        
    }
    NSDate *endDate = [NSDate date];
    
    LotLog(@"time cost:%f",[endDate timeIntervalSinceDate:beginDate]);

    label.frame = orginRect;
   
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
